﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IQualityServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.QualityServices
{
    public class DetectionSettingsServices:IDetectionSettingsServices
    {
        public DetectionSettingsServices(IBaseRepository baseConn)
        {
            BaseConn = baseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(qc_index model)
        {
            var result=await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> DelAsync(string Id)
        {
            var result=await BaseConn.DeleteAsync("delete dbo.qc_index where index_id=@Id", new {Id});
            return result;
        }

        public async Task<bool> DelAsync(string[] Id)
        {
            var result =await BaseConn.DeleteAsync("delete dbo.qc_index where index_id in @Id", new {Id});
            return result;
        }

        public async Task<int> GetCountAsync(string Code, string Name, string Type)
        {
            var sql = "select count(1) from dbo.qc_index where index_code like @Code and index_name like @Name";
            if (!string.IsNullOrEmpty(Type))
            {
                sql += " and index_type like @Type";
            }
            var result=await BaseConn.GetRecoreCountAsync(sql,new{ Code="%"+Code+"%",Name="%"+Name+"%",Type=Type });
            return result;
        }

        public async Task<IEnumerable<qc_index>> GetJsonAsync(int PageIndex, int PageSize, string Code, string Name, string Type)
        {
            var sql = "select * from dbo.qc_index where index_code like @Code and index_name like @Name";
            if(!string.IsNullOrEmpty(Type))
            {
                sql += " and index_type like @Type";
            }
            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            var result = await BaseConn.QueryAsync<qc_index>(sql, new {PageIndex=PageIndex,PageSize=PageSize,Code="%"+Code+"%",Name="%"+Name+"%",Type=Type});
            return result;
        }

        public async Task<bool> UpdateAsync(qc_index model)
        {
            var result=await BaseConn.UpdateAsync(model,"index_id");
            return result;
        }
    }
}
